<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/8
 * Time: 17:33
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Notice extends Base
{
    /**
     * 消息列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->count();// 获取总条数
            $lists = Db::name('notice')
                ->field('n_id, n_title, n_content, n_image, n_type, n_time')
                ->page($Nowpage,$limits)
                ->order('n_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 消息添加
     * @return mixed|\think\response\Json
     */
    public function noticeAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            
            $data = [
                'n_title' => $params['n_title'],//标题
                'n_content' => $params['n_content'],//内容
                'n_image' => $params['n_image'],//缩略图
                'n_time' => date('Y-m-d H:i:s'),//发布时间
                'n_type' => $params['n_type'],//类型
                'n_read' => ',0,',//已读用户
            ];
            $res = Db::table('up_notice')->insert($data);
            if($res){
                //推送
                $this->Jpush(2,'1','您有新的系统消息',['type'=>1]);
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('notice/noticeadd');
    }
    /**
     * 消息编辑
     * @return mixed|\think\response\Json
     */
    public function noticeEdit(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $update = [
                'n_title' => $params['n_title'],//标题
                'n_type' => $params['n_type'],//类型
            ];
            if($params['n_type'] == 1){//文本
                $update['n_content'] = $params['n_content'];
                $update['n_image'] = null;
            }elseif($params['n_type'] == 2){//图片消息
                $update['n_content'] = null;
                if(!empty($params['n_image'])){
                    $update['n_image'] = $params['n_image'];
                }
            }else{//图文消息
                $update['n_content'] = $params['n_content'];
                if(!empty($params['n_image'])){
                    $update['n_image'] = $params['n_image'];
                }
            }
            
            $res = Db::table('up_notice')->where('n_id', $params['n_id'])->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $n_id = input('n_id');
        $info = Db::table('up_notice')
            ->where('n_id', $n_id)
            ->field('n_id, n_title, n_content, n_image, n_type')
            ->find();
        $this->assign('info', $info);
        return $this->fetch('notice/noticeedit');
    }
    
    /**
     * 消息删除
     * @return \think\response\Json
     */
    public function noticeDel(){
        $n_id = input('id');
        $res = Db::table('up_notice')->delete($n_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}